package resfoods.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yc.entity.MemberInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import resfoods.dao.MemberDao;


import java.util.ArrayList;

/**
 * @ClassName JwtUserDetailsService
 * @since: 2023/8/21 11:44
 * @auth: kirito
 * @description:
 **/
@Service
@Slf4j
public class JwtUserDetailsService implements UserDetailsService {
    @Autowired
    private MemberDao resuserDao;
    @Override
    public UserDetails loadUserByUsername(String nickName) throws UsernameNotFoundException {
        QueryWrapper<MemberInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("nickName", nickName);
        MemberInfo resuser = resuserDao.selectOne(wrapper);
        if (resuser != null) {
            return new User(resuser.getNickName(), resuser.getPwd(), new ArrayList<>());
        }else {
            throw new UsernameNotFoundException("user not found with nickName: " + nickName);
        }
    }

    public MemberInfo findByUsername(String username) {
        QueryWrapper<MemberInfo> wrapper=new QueryWrapper();
        wrapper.eq("nickname", username);
        MemberInfo memberInfo = resuserDao.selectOne(wrapper);
        return memberInfo;
    }
}


